package com.example.leetcode.prcatice;

import java.util.Stack;

/**
 * 实现一个MyQueue类，该类用两个栈来实现一个队列。
 */
public class MyQueue {

    private final Stack<Integer> stack ;

    private final Stack<Integer> temp;

    public MyQueue() {
        stack = new Stack<>();
        temp = new Stack<>();
    }

    /** Push element x to the back of queue. */
    public void push(int x) {
        stack.push(x);
    }

    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        if (stack.isEmpty() && temp.isEmpty()){
            return -1;
        }
        if (temp.isEmpty()){
            while (!stack.isEmpty()){
                temp.push(stack.pop());
            }
        }
        return temp.pop();
    }

    /** Get the front element. */
    public int peek() {
        if (stack.isEmpty() ){
            if (temp.isEmpty()){
                return -1;
            }else {
                return temp.peek();
            }
        }else {
            if (!temp.isEmpty()){
               return temp.peek();
            }else {
                while (!stack.isEmpty()){
                    temp.push(stack.pop());
                }
                return temp.peek();
            }
        }
    }

    /** Returns whether the queue is empty. */
    public boolean empty() {
        return stack.isEmpty() && temp.isEmpty();
    }
}


class TestMyQueue{
  public static void main(String[] args) {
    MyQueue queue = new MyQueue();
    queue.push(1);
    queue.push(2);
    System.out.println(queue.peek());
    System.out.println(queue.pop());
    System.out.println(queue.empty());
  }
}